package cn.itcast.wanxinp2p.account.service.Impl;

import cn.itcast.wanxinp2p.account.common.AccountErrorCode;
import cn.itcast.wanxinp2p.account.entity.Account;
import cn.itcast.wanxinp2p.account.mapper.AccountMapper;
import cn.itcast.wanxinp2p.account.service.AccountService;
import cn.itcast.wanxinp2p.api.account.model.AccountDTO;
import cn.itcast.wanxinp2p.api.account.model.AccountLoginDTO;
import cn.itcast.wanxinp2p.api.account.model.AccountRegisterDTO;
import cn.itcast.wanxinp2p.common.domain.RestResponse;
import cn.itcast.wanxinp2p.common.exception.BusinessException;
import cn.itcast.wanxinp2p.common.util.MD5Util;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.util.BeanUtil;
import lombok.extern.slf4j.Slf4j;
import org.dromara.hmily.annotation.Hmily;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.security.auth.login.AccountException;
import java.util.HashMap;

@Slf4j
@Service
public class AccountServiceImpl implements AccountService {

    @Value("${sms.enable}")
    private volatile Boolean smsEnable;
    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private RestTemplate restTemplate;
    @Override
    public RestResponse getSMSCode(String mobile) {
        HashMap<String, String> HashMap = new HashMap<>();
         HashMap.put("mobile", mobile);
         String url="http://localhost:56085/sailing/generate?effectiveTime=300&name=sms";
         return  restTemplate.postForObject(url, HashMap, RestResponse.class);


    }

    @Override
    public Integer checkMobile(String mobile, String key, String code) {
        String url= "http://localhost:56085/sailing/verify?name=sms&verificationKey="+key+"&verificationCode="+code;
        RestResponse forObject = restTemplate.postForObject(url, null, RestResponse.class);
       if (forObject.isSuccessful()){
           new BusinessException(AccountErrorCode.E_140152);
       }else {
           Account ipone= accountMapper.getIpone(mobile);
          if (ipone==null){
                return 0;
          }
       }
        return 1;
    }


    /**
     * 账户注册
     * @param registerDTO 注册信息
     * @return
     */
    @Override
    @Hmily(confirmMethod = "confirmRegister", cancelMethod = "cancelRegister")
    public AccountDTO register(AccountRegisterDTO registerDTO) {
        String mobile = registerDTO.getMobile();
        Account account = new Account();
        AccountDTO accountDTO = new AccountDTO();
        if (accountMapper.getIpone(mobile) == null) {
            BeanUtils.copyProperties(registerDTO, account);
            account.setStatus(1);
            account.setDomain("c");
            account.setPassword(MD5Util.getMd5(account.getMobile()));
            accountMapper.save(account);
//            if (account1>0){
            Account getipone1 = accountMapper.getIpone(mobile);


            BeanUtils.copyProperties(getipone1, accountDTO);
        }
        return accountDTO;
//            throw new BusinessException(AccountErrorCode.E_140141);
//        }
//        throw new BusinessException(AccountErrorCode.E_140141);
//    }


}
    public void confirmRegister(AccountRegisterDTO registerDTO) {
        log.info("execute confirmRegister");
    }

    public void cancelRegister(AccountRegisterDTO registerDTO) {
        log.info("execute cancelRegister");
        //TODO 删除账号
        accountMapper.deleteBymobile(registerDTO.getMobile());
    }

    @Override
    public AccountDTO login(AccountLoginDTO accountLoginDTO) {
        //1、查询用户，注意此处需要根据前端传递的 AccountLoginDTO的domain做判断：
        String domain = accountLoginDTO.getDomain();
        Account account = new Account();
        //  如果值是 c 表示是c端用户，需要根据手机号查询
        if ("c".equals(domain)){
            String mobile = accountLoginDTO.getMobile();
            String password = accountLoginDTO.getPassword();
            account = accountMapper.getUser(mobile,null,password);
        }
        //  否则是b端用户，需要根据用户名查询
        else {
            String username = accountLoginDTO.getUsername();
            String password = accountLoginDTO.getPassword();
            account = accountMapper.getUser(null,username,password);
        }
        //2、判断用户是否存在，如果不存在，直接抛出 AccountErrorCode.E_130104 异常
        if (account==null){
            throw new BusinessException(AccountErrorCode.E_130104);
        }
        AccountDTO accountDTO = new AccountDTO();
        //3、如果 smsEnable=true ，说明是短信验证码登录，不做密码校验。直接返回
        if (smsEnable){
            BeanUtils.copyProperties(account,accountDTO);
            return accountDTO;
        }
        //4、验证密码，如果密码错误则抛出 AccountErrorCode.E_130105 异常
        if(!account.getPassword().equals(MD5Util.getMd5(accountLoginDTO.getPassword()))){
            throw new BusinessException(AccountErrorCode.E_130105);
        }
        BeanUtils.copyProperties(account,accountDTO);
        return accountDTO;
    }
}
